<?php

namespace app\modules\cruise\models;

use Yii;
use yii\imagine\Image;
use Overtrue\Pinyin\Pinyin;
use yii\web\UploadedFile;

/**
 * This is the model class for table "customer_check".
 *
 * @property int $id
 * @property string $first_name 姓
 * @property string $last_name 名
 * @property string $en_first_name 英文姓
 * @property string $en_last_name 英文名
 * @property string $pinyin 名字拼音
 * @property int $gender 1 男 2 女
 * @property string $birthday 出生日期
 * @property string $country 国籍
 * @property string $passport 护照号码
 * @property string $passport_add 护照签发地
 * @property string $passport_date 护照有效期
 * @property string $passport_grant 护照签发日期
 * @property string $passport_img 护照图片
 * @property string $native 出生地
 * @property string $phone 手机号
 * @property string $remark 备注说明
 * @property string $name 姓名
 * @property int $passport_check 护照审核 1 未审核 2 审核通过
 * @property int $user_id 管理员编号
 * @property string $created_at
 * @property string $updated_at
 */
class CustomerCheck extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'customer_check';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['gender', 'passport_check', 'user_id','status'], 'integer'],
            [['birthday', 'passport_date', 'passport_grant', 'created_at', 'updated_at'], 'safe'],
            [['first_name', 'last_name', 'en_first_name', 'en_last_name'], 'string', 'max' => 32],
            [['pinyin', 'passport', 'passport_add', 'passport_img', 'remark'], 'string', 'max' => 255],
            [['country', 'native', 'phone', 'name','group'], 'string', 'max' => 64],
            [['passport'], 'unique'],
        ];
    }

         // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }
         
        $pinyin = new Pinyin();    
        //  图片上传
        $upload = UploadedFile::getInstance($this, "passport_img");

        if($upload){
            $dir = sprintf("uploads/passport/%s",date("Ym"));

            if(!is_readable($dir)) {
                is_file($dir) or mkdir($dir,0755);
            }

            $suffix = $upload->extension;

            $path = sprintf('%s/%s.%s', $dir, uniqid(true), $suffix);

            $ret = $upload->saveAs($path);

            if($ret){
                $this->passport_img = $path;
            }
        }else{
            unset($this->passport_img);
        }
         
        
        // 生成拼音 
        if(empty($this->en_first_name)){
            $this->en_first_name = strtoupper($pinyin->sentence($this->first_name));
        }

        if(empty($this->en_last_name)){
            $this->en_last_name = strtoupper($pinyin->sentence($this->last_name));
        }

        
        $name = sprintf("%s %s", $this->first_name, $this->last_name);
        $name_2 = sprintf("%s%s", $this->first_name, $this->last_name);
        $this->name = $name_2;
        $this->pinyin = $pinyin->sentence($name);
    
        return parent::beforeSave($insert);
    }//}}}

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'first_name' => '中文姓',
            'last_name' => '中文名',
            'en_first_name' => '英文姓',
            'en_last_name' => '英文名',
            'pinyin' => '名字拼音',
            'gender' => '性别',
            'birthday' => '出生日期',
            'country' => '国籍',
            'passport' => '护照号码',
            'passport_add' => '护照签发地',
            'passport_date' => '护照有效期',
            'passport_grant' => '护照签发日期',
            'passport_img' => '护照图片',
            'native' => '出生地',
            'phone' => '手机号',
            'remark' => '备注说明',
            'name' => '姓名',
            'passport_check' => '护照审核',
            'user_id' => 'User ID',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'status' => '状态',
            'group' => '团号',
        ];
    }
}
